package com.example.lawyer.netty.handler;

import com.example.lawyer.netty.NettyConfig;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;


import java.nio.ByteBuffer;

/***
 * TCP长连接编码
 */

public class TcpMessageEncoder extends MessageToByteEncoder<String> {


    public String tokens = "123456789";

    @Override
    public void encode(ChannelHandlerContext ctx, String s, ByteBuf byteBuf) throws Exception {
        byte[] bytes = s.getBytes();
        int length = bytes.length;
        byteBuf.writeInt(NettyConfig.NETTY_TOP_KEY);
        byteBuf.writeInt(length + 4 + 4);
        byte[] bytes1 = tokens.getBytes();
        byteBuf.writeBytes(bytes);
    }
}
